package cn.edu.ujn.lab.controller;

import cn.edu.ujn.lab.dao.Notice;
import cn.edu.ujn.lab.service.INoticeService;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class NoticeController {

    @Autowired
    public INoticeService noticeService;

    //分页
    @GetMapping("/notice/list.action")
    public String list(Model model, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,
                       Notice notice){
        notice.setStart(rows*(page-1));
        notice.setRows(rows);
        Page<Notice> page1 = noticeService.selectAll(notice);
        model.addAttribute("page",page1);
        return "notice";
    }

    //查找
    @RequestMapping("/notice/getNoticeById.action")
    @ResponseBody
    public Notice findById(@PathVariable("id") @RequestParam Integer id){
        return noticeService.findById(id);
    }

    //删除
    @RequestMapping("/notice/delete.action")
    @ResponseBody
    public  Integer deleteById(@PathVariable("id") @RequestParam Integer id){
        return noticeService.delete(id);
    }

    //增加
    @PostMapping("/notice/create.action")
    @ResponseBody
    public Notice add(Notice notice){
        return noticeService.insert(notice);
    }

    //更新
    @PostMapping("/notice/update.action")
    @ResponseBody
    public Notice update(Notice notice){
        return noticeService.update(notice);
    }

    //查找全部
    @GetMapping("/notice")
    @ResponseBody
    public List<Notice> findAll(){
        return noticeService.findAll();
    }

}
